import {
    getSiteInfo
} from "@/utils/storage"
import { 
  getMyBank, 
  getComponentList, 
  getCurComponentList,
  getPresupposeOption,
  myQsStatistics 
} from "@/api/formDesign";
const state = {
  formDesignInfo: {
    label: '', //问卷标题
    labelPlaceholder: '请输入问卷标题',
    description: '', //问卷描述
    formItemLabelWidth: 40,
    formItemList: [], //表单组件信息
    modelValue: {}, //各个组件绑定的值(前端用)
    labelPosition: 'top',
    formCmpId: null,
    allPageNum: null, //总页数
    onePageOneQs: false, //一页一题
    hasPageCmp: false, //是否有分页器
    formNotice: {
      isOpen: false,
      title: '考生须知',
      content: `<div style="font-family: 'sans serif', tahoma, verdana, helvetica; font-size: 14px;"><strong><span style="font-size: 18px;">考试规则</span></strong></div>
      <p>&nbsp;</p>
      <div style="font-family: 'sans serif', tahoma, verdana, helvetica; font-size: 14px;">1、设备要求：请确保设备正常、网络信号良好。</div>
      <p>&nbsp;</p>
      <div style="font-family: 'sans serif', tahoma, verdana, helvetica; font-size: 14px;">2、注意事项：请确保诚信考试，不得以任何方式上网查阅资料。</div>
      <p>&nbsp;</p>
      <div style="font-family: 'sans serif', tahoma, verdana, helvetica; font-size: 14px;"><strong>请各位考生仔细阅读考试须知，因不满足上述条件造成的成绩无效等后果由考生自行承担。</strong></div>
      <p>&nbsp;</p>
      <div style="font-family: 'sans serif', tahoma, verdana, helvetica; font-size: 14px;"><strong>如您同意并认可以上内容，请点击下面「同意」按钮，开始考试。</strong></div>`,
      least: 3,
    },

    publishSetting: { //发布属性
      typeId: null,//发布类型，1调查，2投票
      useLimitTime: false, //是否启用限制有效时间
      limitTime: ['',''], //限制有效时间
      useAuth: false, //是否启用需要登录才能填写 
      useAccessAuth: true,// 是否启用访问权限
      auth: [], //指定角色
      secretKey: '',//授权密钥
      ipGroup: [],//IP安全组
      wifiPosition: false,//wifi定位
      phoneVerify: false,//手机验证
      wechatVerify: false,//微信验证
      useLimitSubmit: false,//是否启用提交次数限制
      limitSubmit: 1,//一个用户提交次数限制
      useLimitIp: false,//是否启用IP限制
      limitIp: [1,1],//一个IP一定时间范围内填写限制
      useLimitDevice: false,//是否启用设备限制
      limitDevice: [1,1],//一个设备一定时间范围内填写限制
      useLimitAnswer: false,//是否启用答卷总数限制
      limitAnswer: 10,//限制最大答卷数
      useLimitCooperate: 0,//协作人限制(0仅自己1所有人2指定人员)
      limitCooperate: [],//指定协作人信息集合
      cooperatePermission: [0,1,2,3,4],//协作人管理权限
      submitText: '提交成功',//提交后文本提示描述
      checkSubmit: false,//提交后能否查看答卷信息
      jumpInfo:{
        description: '提交成功，正在返回首页',
        autoTime: 5,//自动跳转时间
        link: '#',//提交后跳转链接
      },//提交后跳转信息
      onlyCustom: false,//仅支持自定义来源访问
      showSourceName: false,//填写展示来源名称
      loginAddress: null,
      showScore: false,//考试完成是否立即展示成绩
      showAnswer: false,//是否展示答案及解析
      examComment:[],//考试评语
      totalScore:0,//试卷总分
      passingScore:1 //及格分数
    },
    formAppearanceInfo: {
      showLogo: false,//logo导航区展示
      logoImg: getSiteInfo() ? getSiteInfo().logo : null,
      headerImg: null,//页眉图片地址
      backgroundType: 'color',//表单背景类型
      backgroundColor: '',//表单背景颜色
      backgroundImg: null,//表单背景图片
      themeColor: '',//表单组件主题色
    }
  },
  previewFormDesign: {}, //预览的表单信息
  myBank: {}, //我的题库
  curComponentList: [], //当前可用题型
  componentList: [], //当前所有组件
  curId: null, //当前模型id
  curFormId: null, //当前表单问卷id,
  presupposeOption: [], //预定义选项 
  contentStatistics: {}, //我的问卷数量统计
  isExamAudit: false, //是否是审核页面(表单审核和表单设计时预览都用的同一个组件，通过这个字段区分是否显示打分组件)
  testLimit: [1,2,4],
  curBankType:null//当前题目分组类型
}
const mutations = {
  SET_FORMINFO: (state, formDesignInfo) => {
    state.formDesignInfo = formDesignInfo
    state.previewFormDesign = JSON.parse(JSON.stringify(formDesignInfo))
  },
  SET_APPEARANCEINFO: (state, formAppearanceInfo) => {
    state.formDesignInfo.formAppearanceInfo = formAppearanceInfo
  },
  SET_myBank: (state, myBank) => {
    state.myBank = myBank
  },
  SET_curComponentList: (state, curComponentList) => {
    state.curComponentList = curComponentList
  },
  SET_componentList: (state, componentList) => {
    state.componentList = componentList
  },
  SET_previewFormDesign: (state, previewFormDesign) => {
    state.previewFormDesign = previewFormDesign
  },
  SET_PUBLISHSETTING: (state, publishSetting) => {
    state.formDesignInfo.publishSetting = publishSetting
  },
  SET_curId: (state, curId) => {
    state.curId = curId
    state.formDesignInfo.publishSetting.typeId = curId
  },
  SET_curFormId: (state, curFormId) => {
    state.curFormId = curFormId
  },
  SET_presupposeOption: (state, presupposeOption) => {
    state.presupposeOption = presupposeOption
  },
  SET_contentStatistics: (state, contentStatistics) => {
    state.contentStatistics = contentStatistics
  },
  SET_isExamAudit: (state, isExamAudit) => {
    state.isExamAudit = isExamAudit
  },
  SET_curBankType: (state, curBankType) => {
    state.curBankType = curBankType
  },
}

const actions = {
  //获取我的题库
  handleGetMybank(context) {
    return new Promise((resolve, reject) => {
      getMyBank().then(res => {
          if(res.data) {
            context.commit('SET_myBank',res.data)
            resolve(res)
          }
        }).catch(error => {
          reject(error)
        })
    })
  },
  //获取当前可用题型
  handleGetCurComponentList(context, id) {
    return new Promise((resolve, reject) => {
      getCurComponentList({id}).then(res => {
        if(res.data) {
          context.commit('SET_curComponentList',res.data)
          resolve(res)
        }
        }).catch(error => {
          reject(error)
        })
    })
  },
  //获取当前所有组件
  handleGetComponentList(context, id) {
    return new Promise((resolve, reject) => {
      getComponentList({id}).then(res => {
        if(res.data) {
          context.commit('SET_componentList',res.data)
          resolve(res)
        }
        }).catch(error => {
          reject(error)
        })
      })
  },
  //获取预定义选项数据
  handleGetPresupposeOption(context) {
    return new Promise((resolve, reject) => {
      getPresupposeOption().then(res => {
        if(res.data) {
          res.data.forEach(item=>{
            if(item.value.indexOf('\\n' != -1)){
              item.value = item.value.replace(/\\n/g, '\n')
            }
          })
          context.commit('SET_presupposeOption',res.data)
          resolve(res)
        }
        }).catch(error => {
          reject(error)
        })
      })
  },
  //我的问卷数量统计
  handleGetMyQsStatistics(context) {
    return new Promise((resolve, reject) => {
      myQsStatistics().then(res => {
        if(res.data) {
          context.commit('SET_contentStatistics',res.data)
          resolve(res)
        }
        }).catch(error => {
          reject(error)
        })
      })
  },
}
export default {
    namespaced: true, //命名空间
    state,
    mutations,
    actions
}